package cn.clementine.core.api;

import cn.clementine.core.vo.PcapNetworkInterfaceView;
import cn.clementine.core.vo.Result;
import org.pcap4j.core.*;
import org.pcap4j.packet.EthernetPacket;
import org.pcap4j.packet.IllegalRawDataException;
import org.pcap4j.packet.IpV4Packet;
import org.pcap4j.packet.TcpPacket;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.util.List;

@RestController
@RequestMapping("/network")
public class NetworkStatsController {
    @GetMapping("/get_all_devices")
    public Result getAllDevices() throws PcapNativeException {
        var result = Pcaps.findAllDevs().stream().map(e->new PcapNetworkInterfaceView(
                e.getName(),
                e.getDescription(),
                e.getAddresses(),
                e.isLoopBack(),
                e.isUp(),
                e.isRunning()));

        return Result.ok(result);
    }
//    @GetMapping("/startCapture")
//    public String startCapture() {
//        try {
//            List<PcapNetworkInterface> devices = Pcaps.findAllDevs();
//            PcapNetworkInterface selectedDevice = devices.get(0);
//
//            int snapLen = 65536;
//            PcapNetworkInterface.PromiscuousMode mode = PcapNetworkInterface.PromiscuousMode.PROMISCUOUS;
//            int timeout = 10 * 1000;
//            PcapHandle handle = selectedDevice.openLive(snapLen, mode, timeout);
//
//            PacketListener packetListener = packet -> {
//                // 处理数据包，统计流量等
//
//                // 解析数据包
//                EthernetPacket ethernetPacket = null;
//                try {
//                    ethernetPacket = EthernetPacket.newPacket(packet.getRawData(), 0, packet.length());
//                } catch (IllegalRawDataException e) {
//                    throw new RuntimeException(e);
//                }
//                IpV4Packet ipV4Packet = ethernetPacket.get(IpV4Packet.class);
//                TcpPacket tcpPacket = ipV4Packet.get(TcpPacket.class);
//
//                // 统计上行和下行流量
//                InetAddress sourceAddress = ipV4Packet.getHeader().getSrcAddr();
//                InetAddress destinationAddress = ipV4Packet.getHeader().getDstAddr();
//                int sourcePort = tcpPacket.getHeader().getSrcPort().value();
//                int destinationPort = tcpPacket.getHeader().getDstPort().value();
//
//                if (sourceAddress.equals(selectedDevice.getAddresses().get(0).getAddress())) {
//                    // 上行流量
//                    System.out.println("上行流量：源地址=" + sourceAddress + ", 源端口=" + sourcePort +
//                            ", 目的地址=" + destinationAddress + ", 目的端口=" + destinationPort);
//                    // 更新上行流量统计信息
//                    // ...
//                } else {
//                    // 下行流量
//                    System.out.println("下行流量：源地址=" + sourceAddress + ", 源端口=" + sourcePort +
//                            ", 目的地址=" + destinationAddress + ", 目的端口=" + destinationPort);
//                    // 更新下行流量统计信息
//                    // ...
//                }
//
//                // 输出统计结果
//                // ...
//            };
//
//            int maxPackets = -1;
//            handle.loop(maxPackets, packetListener);
//        } catch (Exception e) {
//            // 处理异常
//        }
//
//        return "Network traffic capture started.";
//    }
}